package com.gin.kotlin.no_gay_alliance.activity.pm

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.FragmentStateContainer
import com.gin.kotlin.no_gay_alliance.databinding.ActivityPrivateMessageBinding
import com.gin.kotlin.no_gay_alliance.fragment.pm.PrivateMessageBlackListFragment
import com.gin.kotlin.no_gay_alliance.fragment.pm.PrivateMessageListFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class PrivateMessageActivity : AbstractBindingActivity<ActivityPrivateMessageBinding>(), TabLayoutMediator.TabConfigurationStrategy {
    private lateinit var tabLayoutMediator: TabLayoutMediator
    private lateinit var fragmentStateContainers: List<FragmentStateContainer>

    override fun onCreate() {
        val b = this.b ?: throw RuntimeException("启动错误")
       this.fragmentStateContainers = listOf(
            FragmentStateContainer("私信") { PrivateMessageListFragment() },
            FragmentStateContainer("黑名单") { PrivateMessageBlackListFragment() },
        )
        // 为ViewPager2设置适配器
        b.viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = fragmentStateContainers.size
            override fun createFragment(position: Int) = FragmentStateContainer.newInstance(fragmentStateContainers, position)
        }

        this.tabLayoutMediator = attachTabLayout(b.tabLayout, b.viewPager2, this)

    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, menuInflater)
        menuInflater.inflate(R.menu.private_message_list,menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId==R.id.mi_private_message_new){
            Log.d(TAG, "发起新私信")
            PrivateMessageNewActivity.start(this)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun handleIntent(intent: Intent) {
    }

    override fun onResume() {
        super.onResume()
        setToolbarTitle("私信")
    }

    companion object {
        fun startActivity(context: Context) = context.startActivity(Intent(context, PrivateMessageActivity::class.java))

    }

    override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
        tab.text = fragmentStateContainers[position].title
    }
}